﻿using Castle.Core.Logging;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace WebAppHappyNewYear.Filter
{
    public class CustomCacheResourseFilterAttribute : Attribute, IResourceFilter, IFilterMetadata, IOrderedFilter
    {

        private static Dictionary<string, IActionResult> CustomCacheResourseDictionary = new Dictionary<string, IActionResult>();

        public int Order { get; set; }

        public void OnResourceExecuted(ResourceExecutedContext context)
        {
            CustomCacheResourseDictionary.Add(context.HttpContext.Request.Path, context.Result);
        }

        public void OnResourceExecuting(ResourceExecutingContext context)
        {
            
            string key = context.HttpContext.Request.Path;
            if (CustomCacheResourseDictionary.ContainsKey(key))
            {
                //Result 是MVC 的短路器，一旦 Result 被制定，那么 mvc 流程就结束
                context.Result = CustomCacheResourseDictionary[key];
            }
        }
    }


}
